'''
Describe data set with form
Age,Sex,BMI,Weight,Years Smoking

Give mean, median, var, range, iqr
'''

def calculateMedian(data):
    ''' Calculate the median of a list '''
    data.sort()
    if len(data) % 2 == 0:
        return (data[len(data) / 2] + age[len(data)/2+1]) / 2
    else:
        return  data[len(data) / 2]

def calculateVariance(data):
    ''' Calculate variance using exp(x^2) - exp(x)^2 '''
    expSquares = 0.0
    exp = 0.0
    for n in data:
        exp += n
        expSquares += n*n

    expSquares /= len(data)
    exp /= len(data)
    return expSquares - exp*exp

#####################################################################

import sys
if len(sys.argv) < 2:
    print "Usage: python describe_data.py <file>" 
    sys.exit(1)


fileName = sys.argv[1]
fileHandle = open(fileName,'r')

header = fileHandle.readline()

age = []
gender = []
bmi = []
weight = []
yrsSmoking = []

ageM = []
genderM = []
bmiM = []
weightM = []
yrsSmokingM = []

ageF = []
genderF = []
bmiF = []
weightF = []
yrsSmokingF = []

lineNum = 1
for line in fileHandle:
    # tokenize
    tokens = line.rstrip().split(',')
    age.append(float(tokens[0]))
    gender.append(tokens[1])
    bmi.append(float(tokens[2]))
    weight.append(float(tokens[3]))
    yrsSmoking.append(float(tokens[4]))

    if tokens[1] == 'M':

        ageM.append(float(tokens[0]))
        genderM.append(tokens[1])
        bmiM.append(float(tokens[2]))
        weightM.append(float(tokens[3]))
        yrsSmokingM.append(float(tokens[4]))

    elif tokens[1] == 'F':

        ageF.append(float(tokens[0]))
        genderF.append(tokens[1])
        bmiF.append(float(tokens[2]))
        weightF.append(float(tokens[3]))
        yrsSmokingF.append(float(tokens[4]))

    else:
        print "Error in data, line ", lineNum
        sys.exit(1)

    lineNum+=1

print "N: %d %d %d" %(len(age), len(ageM), len(ageF))

print "Age:"
print "\tmean: %f %f %f" %(sum(age) / len(age), sum(ageM) / len(ageM), sum(ageF) / len(ageF))

# Calculate median
print "\tmedian: %f %f %f" % ( calculateMedian(age), calculateMedian(ageM), calculateMedian(ageF)) 
# Std dev
print "\tvar: %f %f %f" % ( calculateVariance(age), calculateVariance(ageM), calculateVariance(ageF)) 
# Range
print "\tRange total: %f - %f" % ( min(age), max(age)  )
print "\tRange male: %f - %f" % ( min(ageM), max(ageM)  )
print "\tRange female: %f - %f" % ( min(ageF), max(ageF)  )



